from django.db import models
#用户表
class User(models.Model):
    ON = 1
    OFF = 2
    STATUS_CHOICES = {
        (ON,'正常'),
        (OFF,'锁定')
    }
    #用户名，长度30，不允许重复
    class Meta:
        verbose_name_plural = '用户列表'
    user_name=models.CharField(max_length=30,unique=True,verbose_name="用户名")
    pwd = models.CharField(max_length=200,verbose_name="密码")
    status = models.IntegerField(choices=STATUS_CHOICES,default=ON,verbose_name="状态",)
    phone = models.CharField(max_length=30,unique=True,verbose_name="手机号")
    list_display_links = ['user_name']
    def __str__(self):
        return self.user_name
#收货地址
class Address(models.Model):
    class Meta:
        verbose_name_plural = '收货地址'
    user = models.ForeignKey('user',on_delete=models.CASCADE)
    user_name = models.CharField(max_length=20,unique=True)
    phone = models.CharField(max_length=30)
    address = models.CharField(max_length=200,null=False)
    is_default = models.IntegerField(default=1)

    def __str__(self):
        return self.address + "(" + self.user_name + ")收，联系电话：" + self.phone
#商品表
class Goods(models.Model):
    class Meta:
        verbose_name_plural = '商品表'
    good_name = models.CharField(max_length=30,verbose_name="商品名称")
    img_url = models.ImageField(upload_to='img',verbose_name="商品图片")
    price = models.IntegerField(verbose_name="商品数量")
    intro = models.TextField(verbose_name="商品介绍")
    type = models.ManyToManyField('Type',null=True,verbose_name="商品单位")
    K = "克"
    J = "斤"
    G = "个"
    UNIT_CHOICES = {
        (K, "克"),
        (J, "斤"),
        (G, "个")
    }
    unit = models.CharField(choices=UNIT_CHOICES,default="克",max_length=10,verbose_name="商品单位")
    object = models.Manager()

    def __str__(self):
        return self.good_name
#商品分类
class Type(models.Model):
    class Meta:
        verbose_name_plural = '商品分类'
    type_name= models.CharField(max_length=30,unique=True)
    def __str__(self):
        return self.type_name
#购物车
class Cart(models.Model):
    class Meta:
        verbose_name_plural = '购物车列表'
    num = models.IntegerField(verbose_name="购买数量")
    user = models.ForeignKey("User",on_delete=models.CASCADE)
    goods = models.ManyToManyField(Goods)
#订单表
class Order(models.Model):
    class Meta:
        verbose_name_plural = '订单表'
    order_no = models.CharField(max_length=30,unique=True)
    status = models.IntegerField()
    time = models.DateTimeField(auto_now_add=True)
    user_name = models.CharField(max_length=20)
    phone = models.CharField(max_length=30)
    address = models.CharField(max_length=200)
    user = models.ForeignKey("User",on_delete=models.CASCADE)
    total_price = models.IntegerField()
#订单详情表
class OrderDetial(models.Model):
    class Meta:
        verbose_name_plural = '订单详情表'
    good_name = models.CharField(max_length=30)
    price = models.IntegerField()
    num = models.IntegerField()
    img_url = models.CharField(max_length=100)
    total_price = models.IntegerField()
    order = models.ForeignKey('Order',on_delete=models.CASCADE)
# Create your models here.
